<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t("公司管理") }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('公司全称')" prop="companyFullName">
        <el-input
          v-model="queryParams.companyFullName"
          :placeholder="$t('公司全称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('公司简称')" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          :placeholder="$t('公司简称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <!--      <el-form-item :label="$t('公司分类')" prop="companyClass">-->
      <!--        <dictSelect-->
      <!--          v-model="queryParams.companyClass"-->
      <!--          :clearable="true"-->
      <!--          dictionary="company_class"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="queryHandle"
        >{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
          $t("重置")
        }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:company:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:company:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t('导出') }}</el-button>
      </el-col> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="dealCompanyList"
      border
      tooltip-effect="darkTable"
    >
      <el-table-column
        :label="$t('序号')"
        type="index"
        align="center"
        show-overflow-tooltip
        width="50"
      >
        <template slot-scope="scope">
          {{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('公司全称')"
        align="center"
        prop="companyFullName"
        show-overflow-tooltip
        width="220px"
      >
        <template slot-scope="scope">
          <router-link
            target="_blank"
            :to="{
              path: '/company/base/companyBaseHome',
              query: { companyId: scope.row.companyId },
            }"
            class="link_a"
          >{{ scope.row.companyFullName }}</router-link>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('公司简称')"
        align="center"
        prop="company.companyName"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.company.companyName }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column-->
      <!--        :label="$t('公司分类')"-->
      <!--        align="center"-->
      <!--        prop="companyClassLabel"-->
      <!--      />-->
      <el-table-column
        :label="$t('企业统一社会信用代码')"
        align="center"
        prop="company.creditCode"
        width="180px"
      />
      <el-table-column
        :label="$t('法人代表')"
        align="center"
        prop="company.legalPerson"
      />
      <el-table-column
        :label="$t('一级行业')"
        align="center"
        prop="company.industryFirst"
      />
      <el-table-column
        :label="$t('二级行业')"
        align="center"
        prop="company.industrySecond"
      />
      <el-table-column
        :label="$t('注册地-省')"
        align="center"
        prop="company.provinceRegistered"
      />
      <el-table-column
        :label="$t('注册地-市')"
        align="center"
        prop="company.cityRegistered"
      />
      <el-table-column
        :label="$t('注册资本') + $t('金额单位')"
        align="center"
        prop="company.capitalRegistered"
      >
        <template slot-scope="scope">
          <!-- {{
            (scope.row.company.capitalRegistered / 10000) | moneyFormatNewest
          }}
          万元 -->
          <span class="money-text">{{ moneyFormat(scope.row.company.capitalRegistered, "w") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('操作')"
        align="center"
        class-name="small-padding fixed-width"
        width="160px"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:company:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t("修改") }}</el-button>
          <el-button
            v-hasPermi="['system:company:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t("删除") }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公司管理对话框 -->
    <el-dialog
      :title="$t(title)"
      :visible.sync="formOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="dialogForm"
      append-to-body
    >
      <dealCompanyForm ref="dealCompanyForm" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listDealCompany,
  delDealCompany,
  exportDealCompany
} from '@/api/deal/estate/company/dealCompany'
import dealCompanyForm from './dealCompanyForm.vue'
export default {
  components: {
    dealCompanyForm
  },
  filters: {
    // 全局过滤器金额
    moneyFormatNewest: function(val) {
      if (val === '' || val === undefined || val === null) {
        return ''
      }
      // 默认保留三位小数、超过四舍五入 //returnval.toLocaleString()
      let s = val
      let n = 2
      n = n > 0 && n <= 20 ? n : 2
      s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(n) + ''
      var res = s
        .split('.')[0]
        .toString()
        .replace(/\d+/, function(n) {
          // 先提取整数部分
          return n.replace(/(\d)(?=(\d{3})+$)/g, function($1) {
            return $1 + ','
          })
        })
      return res + '.' + s.split('.')[1]
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 公司管理表格数据
      dealCompanyList: [],
      // 弹出层标题
      title: '公司管理',
      // 是否显示弹出层
      formOpen: false,
      // 查询参数
      queryParams: {
        dealId: this.$route.query.id,
        companyFullName: undefined,
        companyName: undefined,
        companyClass: undefined,
        companyCode: undefined,
        legalPerson: undefined,
        firstBusiness: undefined,
        twoBusiness: undefined,
        province: undefined,
        city: undefined,
        address: undefined,
        foundingTime: undefined,
        registerCapital: undefined,
        currency: undefined,
        realityCapital: undefined,
        capitalCurrency: undefined,
        abstracts: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询公司管理列表 */
    getList() {
      this.loading = true
      listDealCompany(this.queryParams).then((response) => {
        this.dealCompanyList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,

        dealId: undefined,

        companyFullName: undefined,

        companyName: undefined,

        companyClass: undefined,

        companyCode: undefined,

        legalPerson: undefined,

        firstBusiness: undefined,

        twoBusiness: undefined,

        province: undefined,

        city: undefined,

        address: undefined,

        foundingTime: undefined,

        registerCapital: undefined,

        currency: undefined,

        realityCapital: undefined,

        capitalCurrency: undefined,

        abstracts: undefined,

        createBy: undefined,

        createTime: undefined,

        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.dealCompanyForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.dealCompanyForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      })
        .then(function() {
          return delDealCompany(row.id)
        })
        .then(() => {
          this.getList()
          this.msgSuccess(this.$t('删除成功'))
        })
        .catch(function() {})
    },
    /** 导出按钮操作 */
    exportHandle() {
      const queryParams = this.queryParams
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      })
        .then(function() {
          return exportDealCompany(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
        })
        .catch(function() {})
    }
  }
}
</script>
